<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <Bread separator="/">
        <BreadItem to="/">首页</BreadItem>
        <BreadItem>{{ categoryName }}</BreadItem>
      </Bread>
      <!-- 轮播图 -->
      <Slider :data="list" style="height: 500px" />
      <!-- 全部分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in allCategoryList" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in allCategoryList" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <More to="/" />
        </div>
        <div class="body">
          <GoodItem
            v-for="good in item.goods"
            :key="good.id"
            :good="good"
          ></GoodItem>
          <!-- <RouterLink
            :to="`/good/${good.id}`"
            class="goods-item"
            v-for="good in item.goods"
            :key="good.id"
          >
            <img :src="good.picture" alt="" />
            <p class="name ellipsis">{{ good.name }}</p>
            <p class="desc ellipsis">{{ good.desc }}</p>
            <p class="price">&yen;{{ good.price }}</p>
          </RouterLink> -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodItem from './components/good-item.vue'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { getAllCategoryApi } from '@/api/category.js'
import { useSlider } from '@/hooks/slider'
export default {
  name: 'TopCategory',
  components: {
    GoodItem
  },
  setup () {
    // 当前分类名
    const categoryName = ref('')
    // banner功能
    const { list } = useSlider()
    const allCategoryList = ref([])
    const route = useRoute()
    const getAllCategory = async () => {
      const { result } = await getAllCategoryApi(route.params.id)
      // console.log(result)
      categoryName.value = result.name
      allCategoryList.value = result.children
    }
    getAllCategory()
    return { list, allCategoryList, categoryName }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
